'use strict'

define(['zepto', 'ctx', 'sui'], function($, ctx) {
  return {
    init: function() {
      
      $.init();
      
      this.music();
      this.loading();
      this.initSubmit();
    },

    loading: function() {
      var starNum = 0;
      var clearIntervalId = setInterval(function() {
        starNum++;
        $(".progress-bar").width(starNum + "%");
        $(".progress-num").text(starNum + "%");
        if(starNum >= 100) {
          clearInterval(clearIntervalId)
          $.router.load("#bmi-index", true)
        }
      }, 26)
    },

    music: function() {

        function forceSafariPlayAudio() {
            audioEl.load(); // iOS 9   还需要额外的 load 一下, 否则直接 play 无效
            audioEl.play(); // iOS 7/8 仅需要 play 一下
        }

        var audioEl = document.getElementById('bgmusic');
        // 可以自动播放时正确的事件顺序是
        // loadstart
        // loadedmetadata
        // loadeddata
        // canplay
        // play
        // playing
        // 
        // 不能自动播放时触发的事件是
        // iPhone5  iOS 7.0.6 loadstart
        // iPhone6s iOS 9.1   loadstart -> loadedmetadata -> loadeddata -> canplay
        audioEl.addEventListener('loadstart', function() {
            console.log('loadstart');
        }, false);

        audioEl.addEventListener('loadeddata', function() {
            console.log('loadeddata');
        }, false);

        audioEl.addEventListener('loadedmetadata', function() {
          console.log('loadedmetadata');
        }, false);

        audioEl.addEventListener('canplay', function() {
            console.log('canplay');
        }, false);

        audioEl.addEventListener('play', function() {
            console.log('play');
            // 当 audio 能够播放后, 移除这个事件
            window.removeEventListener('touchstart', forceSafariPlayAudio, false);
        }, false);

        audioEl.addEventListener('playing', function() {
            console.log('playing');
        }, false);

        audioEl.addEventListener('pause', function() {
            console.log('pause');
        }, false);

        // 由于 iOS Safari 限制不允许 audio autoplay, 必须用户主动交互(例如 click)后才能播放 audio,
        // 因此我们通过一个用户交互事件来主动 play 一下 audio.
        window.addEventListener('touchstart', forceSafariPlayAudio, false);
        audioEl.src = 'static/music/bg.mp3';
    },

    initSubmit: function() {
      var submitButton = $("a.btn-submit");
      var that = this;

      submitButton.on('click', function(e){
        e.preventDefault();
        if(that.validate()) {
          var bmi = that.calculateBmi().toFixed(1);
          console.log($("img.result-image"))
          if(bmi <= 18) {
            $(".surprise-block").eq(2).show().siblings().hide();
          }else if(bmi > 22) {
             $(".surprise-block").eq(0).show().siblings().hide();
          }else {
             $(".surprise-block").eq(1).show().siblings().hide();
          }

          $.router.load("#bmi-result", true)
        }
      })

    },

    validate: function() {
      var sex = $("input[name='gender']").val();
      var height = $("input[name='height']").val();
      var weight = $("input[name='weight']").val();
      
      if(sex != '男' && sex != '女') {
        $.toast('请输入正确的性别（男或女）');
        return false;
      }

      if(height == '' || height == null || height== undefined) {
        $.toast("请输入身高");
        return false;
      }

      var reg = new RegExp("^[0-9]*$");

      if(!reg.test(height)) {
        $.toast("请输入正确的身高，精确到cm");
        return false;
      }

      if(weight == '' || weight == null || weight== undefined) {
        $.toast("请输入体重");
        return false;
      }


      if(!reg.test(height)) {
        $.toast("请输入正确的体重，精确到kg");
        return false;
      }

      return true;
    },

    calculateBmi: function() {
      //Obtain user inputs
      var height=Number($("input[name='height']").val());
      var weight=Number($("input[name='weight']").val());
      
      if(height && weight) {
        //Perform calculation
        var BMI=weight/Math.pow((height / 100),2);
        return BMI;
      }else {
        return 0;
      }
    }
  }
})
